from time import sleep

import allure

from common.airtest.airtest_base import AirtestBase, exists_1
from common.log.log import log
from config.conf import BASE_DIR
from po.jinhua.applets_po.evaluation_page import EvaluationPage
from po.jinhua.applets_po.shopping_cart.shopping_cart_page import ShoppingCartPage
from po.jinhua.applets_po.submit_order_page import SubmitOrderPage


class ProductDetailsPage:
    """
    商品详情页
    """

    def __init__(self):
        self.drive = AirtestBase()

    @allure.step('商品详情页，点击收藏')
    def collection(self):
        """商品详情页，点击收藏"""
        try:
            self.drive.click_5(BASE_DIR + r"/element_picture/商品详情页/收藏.png")
            return ProductDetailsPage()
        except Exception as e:
            log.error(f'收藏商品出现异常，为：{e}')
            raise e

    @allure.step('商品详情页，返回首页')
    def back_to_home(self):
        """返回首页"""
        AirtestBase().go_home(mode=1)

    @allure.step('商品详情页，打开用户评价')
    def open_user_evaluation(self):
        """
        商品详情页，打开用户评价
        """
        try:
            log.info('商品详情页，打开用户评价')
            element = BASE_DIR + r"/element_picture/商品详情页/好评.png"
            AirtestBase().up_down_search_element_and_click(element)
            return EvaluationPage()
        except Exception as e:
            log.error(f'商品详情页，打开用户评价出现异常，为：{e}')
            raise e

    @allure.step('商品详情页，将商品加入购物车')
    def add_to_cart(self):
        """
        商品详情页，将商品加入购物车
        """
        try:
            log.info('商品详情页，将商品加入购物车')
            element_1 = BASE_DIR + r"/element_picture/商品详情页/加入购物车.png"
            element_2 = BASE_DIR + r"/element_picture/商品详情页/规格的左侧一点点按钮.png"
            element_3 = BASE_DIR + r"/element_picture/商品详情页/规格按钮右侧空白区较长.png"
            AirtestBase().click_5(element_1)

            # for i in range(5):
            #     if exists_1(element_2):
            #         log.info('选择商品规格')
            #         AirtestBase().click_4(element_2)  # 恢复默认0.75，降低准确度
            #         sleep(0.5)
            AirtestBase().click_4(element_2)  # 恢复默认0.75，降低准确度

            AirtestBase().click_5(element_1)
            return ProductDetailsPage()
        except Exception as e:
            log.error(f'商品详情页，将商品加入购物车出现异常，为：{e}')
            raise e

    @allure.step('商品详情页，直接进入购物车页面')
    def details_page_go_to_shopping_cart(self):
        """
        商品详情页，直接进入购物车页面
        """
        try:
            log.info('商品详情页，直接进入购物车页面')
            element_1 = BASE_DIR + r"/element_picture/商品详情页/购物车.png"
            AirtestBase().click_5(element_1)
            return ShoppingCartPage()
        except Exception as e:
            log.error(f'商品详情页，直接进入购物车页面出现异常，为：{e}')
            raise e

    @allure.step('商品详情页，立即购买')
    def product_details_buy_now(self):
        """
        商品详情页，立即购买
        """
        try:
            log.info('商品详情页，立即购买')
            element_1 = BASE_DIR + r"/element_picture/商品详情页/立即购买.png"
            element_2 = BASE_DIR + r"/element_picture/商品详情页/规格的左侧一点点按钮.png"
            element_3 = BASE_DIR + r"/element_picture/商品详情页/规格按钮右侧空白区较长.png"
            AirtestBase().click_5(element_1)
            for i in range(5):
                if exists_1(element_3):
                    log.info('选择商品规格。')
                    AirtestBase().click_4(element_3)  # 恢复默认0.75，降低准确度
                    sleep(1)
            AirtestBase().click_5(element_1)
            return SubmitOrderPage()
        except Exception as e:
            log.error(f'商品详情页，立即购买出现异常，为：{e}')
            raise e
